<template>
	<view class="page">
		<!-- <view class="top-tool">
			<image class="refresh-icon" src="/static/images/renovate@2x.png" @click="renovate"></image>
			<view class="pull-down-btn" @click="chooseStatus">
				<view class="text">{{ optionValue }}</view>
				<image v-if="!isShow" class="icon" src="/static/images/ic_drops.png"></image>
				<image v-else class="icon1" src="/static/images/shangla.png"></image>
			</view>
		</view>
		<view v-show="isShow" class="ep-select-content-wrap">
			<scroll-view class="ep-select-content" :scroll-y="true">
				<view v-for="(item, index) in options" :key="index" class="option-item" @click="itemClick(item, index)">
					{{ item }}
				</view>
			</scroll-view>
			<text class="triangle"></text>
		</view> -->
		<!-- 订单菜单 -->
		<u-sticky>
			<view class="tab-list">
				<view class="tab-item" v-for="(item, index) in tabs" :key="index" @click="tabChange(item, index)">
					<view class="value" :class="current == index ? 'active' : ''">{{ item.name }}</view>
					<view class="line" v-if="current == index"></view>
				</view>
			</view>
		</u-sticky>
		<!-- 订单列表 -->
		<view class="order-list" v-if="list.length > 0">
			<view class="order-item" v-for="(item, index) in list" :key="index">
				<view class="top-info">
					<view class="type">{{ item.serviceName || '--' }}</view>
				</view>
				<view class="serve-user" v-if="item.patientName">
					服务对象：
					<text>{{ item.patientName || '--' }}</text>
				</view>
				<view class="serve-user">
					就诊医院：
					<text>{{ item.hospitalName || '--' }}</text>
				</view>
				<view class="serve-user" v-if="item.treatmentTime">
					服务时间：
					<text>{{ item.treatmentTime }}</text>
				</view>
				<view class="serve-user" style="padding-bottom: 10rpx;" v-if="item.serviceRequirements">
					服务需求：
					<text>{{ item.serviceRequirements }}</text>
				</view>
				<view class="bottom-info">
					<view class="cancel-pay" @click="toDetail(item)">查看详情</view>
					<view class="now-start" @click="begin(item)" v-if="current == 0">开始服务</view>
					<view class="cancel-pay" v-if="current == 1" @click="goPage(item)">服务完成</view>
				</view>
			</view>
		</view>
		<view class="empty-view" v-else>暂无订单</view>
		<!-- 底部自定义tab栏 -->
		<view class="fixed-bottom">
			<view>
				<image class="tab-icon" src="/static/tabs/nav_three_active.png"></image>
				<view class="tab-text" style="color: #32C59A;">首页</view>
			</view>
			<view @click="toPerson">
				<image class="tab-icon" src="/static/tabs/nav_five.png"></image>
				<view class="tab-text" style="color: #A3A1A1;">个人中心</view>
			</view>
		</view>
	</view>
</template>

<script>
import { getOrderPageList, serviceStart, updateStatus, getEscortInfo } from '@/apis/request.js';

export default {
	data() {
		return {
			current: 0,
			tabs: [
				{
					name: '待服务'
				},
				{
					name: '服务中'
				},
				{
					name: '已完成'
				}
			],
			select: '',
			options: ['可接单', '休息'],
			optionValue: '可接单',
			isShow: false,
			parmes: {
				pageNo: 1,
				pageSize: 10,
				status: 1,
				type: 1
			},
			total: 0,
			list: [],
			user: {}
		};
	},
	onLoad() {},
	onShow() {
		this.init();
	},
	onPullDownRefresh() {
		this.init();
	},
	onReachBottom() {
		if (this.total > this.list.length) {
			this.parmes.pageNo++;
			this.getList();
		}
	},
	methods: {
		init() {
			this.parmes.pageNo = 1;
			this.getList();
			//this.userInfo();
		},
		//获取陪诊师信息
		userInfo() {
			getEscortInfo().then(res => {
				this.user = res;
				if (res.status == 0) {
					this.optionValue = '休息';
				} else {
					this.optionValue = '可接单';
				}
			});
		},
		//获取列表
		getList() {
			getOrderPageList(this.parmes)
				.then(res => {
					this.total = res.total;
					if (this.parmes.pageNo == 1) {
						this.list = res.records;
					} else {
						this.list = this.list.concat(res.records);
					}
				})
				.finally(() => {
					uni.stopPullDownRefresh();
				});
		},
		//开始服务
		begin(item) {
			let that = this;
			uni.showModal({
				title: '提示',
				content: '是否开始服务',
				success: function(res) {
					if (res.confirm) {
						serviceStart({
							orderId: item.id
						}).then(res => {
							that.parmes.pageNo = 1;
							that.getList();
						});
					} else if (res.cancel) {
						console.log('用户点击取消');
					}
				}
			});
		},
		//服务完成跳转
		goPage(item) {
			uni.navigateTo({
				url: '/pages/attendantPages/serviceComplete?orderId=' + item.id
			});
		},
		//刷新列表
		renovate() {
			this.init();
		},
		tabChange(item, index) {
			this.current = index;
			if (index == 0) {
				this.parmes.status = 1;
			} else if (index == 1) {
				this.parmes.status = 2;
			} else if (index == 2) {
				this.parmes.status = 3;
			}
			this.init();
		},
		toPerson() {
			uni.redirectTo({
				url: '/pages/attendantPages/personalCenter'
			});
		},
		//选择接单的状态
		itemClick(item, index) {
			this.optionValue = item;
			this.isShow = false;
			updateStatus().then(res => {
				this.$Toast('接单状态切换成功');
			});
		},
		//选择接单
		chooseStatus() {
			this.isShow = !this.isShow;
		},
		//查看详情
		toDetail(item) {
			uni.navigateTo({
				url: '/pages/attendantPages/orderDetails?orderId=' + item.id
			});
		}
	}
};
</script>

<style lang="scss" scoped>
.page {
	background-color: #f2f2f2ff;
	height: 100%;
	min-height: calc(100vh - 130upx);
	padding-bottom: 130rpx;

	.top-tool {
		width: 750rpx;
		box-sizing: border-box;
		padding: 20rpx 30rpx 20rpx 0;
		display: flex;
		align-items: center;
		justify-content: flex-end;
		background-color: #fff;

		.refresh-icon {
			width: 42rpx;
			height: 36rpx;
			margin-right: 20rpx;
		}

		.pull-down-btn {
			display: flex;
			align-items: center;
			justify-content: space-around;
			border: 2rpx solid #dcdcdc;
			width: 240rpx;
			height: 60;
			line-height: 60rpx;
			text-align: center;
			border-radius: 34rpx;
			box-sizing: border-box;
			padding: 0 30rpx;

			.text {
				margin-right: 10rpx;
				font-size: 28rpx;
			}

			.icon {
				width: 24rpx;
				height: 14rpx;
			}

			.icon1 {
				width: 24rpx;
				height: 20rpx;
			}
		}
	}

	.tab-list {
		background-color: #fff;
		border-top: 2rpx solid #f1f1f1;
		width: 750rpx;
		background-color: #fff;
		display: flex;
		align-items: center;
		justify-content: space-around;

		.tab-item {
			display: flex;
			flex-direction: column;
			align-items: center;
			font-size: 28rpx;
			color: #999999;

			.value {
				padding: 30rpx 0 24rpx 0;
			}

			.line {
				width: 28rpx;
				height: 6rpx;
				border-radius: 3rpx;
				background: #32C59A;
			}
		}

		.active {
			font-size: 30rpx;
			color: #020202;
		}
	}

	.order-list {
		.order-item {
			margin-top: 20rpx;
			background-color: #fff;
			width: 750rpx;
			box-sizing: border-box;
			padding: 30rpx 30rpx 20rpx 30rpx;

			.top-info {
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding-bottom: 8rpx;

				.type {
					font-size: 30rpx;
				}

				.state {
					font-size: 28rpx;
				}
			}

			.serve-user {
				margin: 14rpx 0 20rpx 0;
				font-size: 26rpx;
				color: #999999;

				text {
					color: #020202;
				}
			}

			.bottom-info {
				border-top: 2rpx solid #ececec;
				padding-top: 20rpx;
				display: flex;
				align-items: center;
				justify-content: flex-end;

				.now-start {
					margin-left: 20rpx;
					width: 150rpx;
					height: 50rpx;
					line-height: 50rpx;
					border-radius: 26rpx;
					background-color: #32C59A;
					color: #fff;
					font-size: 26rpx;
					text-align: center;
					box-sizing: border-box;
				}

				.cancel-pay {
					margin-left: 20rpx;
					width: 150rpx;
					height: 50rpx;
					line-height: 50rpx;
					border-radius: 26rpx;
					color: #999999;
					font-size: 26rpx;
					text-align: center;
					border: 2rpx solid #dddddd;
					box-sizing: border-box;
				}
			}
		}
	}

	.fixed-bottom {
		box-shadow: rgba(17, 17, 26, 0.05) 0px 1px 0px, rgba(17, 17, 26, 0.1) 0px 0px 8px;
		position: fixed;
		bottom: 0rpx;
		left: 0;
		right: 0;
		height: 102rpx;
		display: flex;
		align-items: center;
		justify-content: space-around;
		text-align: center;
		background-color: #fff;

		.tab-icon {
			height: 42rpx;
			width: 44rpx;
		}

		.tab-text {
			font-size: 22rpx;
		}
	}
}

.ep-select-content-wrap {
	width: 230rpx;
	position: absolute;
	top: 45px;
	right: 36rpx;
	z-index: 9999;
	padding-top: 6px;

	.ep-select-content {
		background-color: #fff;
		padding: 8px 0;
		box-sizing: border-box;
		font-size: 14px;
		box-shadow: rgba(100, 100, 111, 0.2) 0px 7px 29px 0px;
		border-radius: 5px;
		max-height: 181px;

		.option-item {
			padding: 8px 18px;
			cursor: pointer;

			&:hover {
				background-color: #f5f7fa;
			}
		}

		.active {
			color: #007aff;
		}

		.disabled {
			color: #c0c4cc;

			&:hover {
				background-color: #f5f7fa;
			}
		}
	}
}

.empty-view {
	height: calc(100vh - 206upx);
}
</style>
